import React, { Component } from 'react';
import { WeaTableNew, WeaSwitch } from 'comsMobx';
import { WeaCheckbox, WeaLocaleProvider, WeaTools, WeaErrorPage, WeaTab, WeaSearchGroup, WeaFormItem } from 'ecCom';
import { toJS } from 'mobx';
import OdocSignInput from './public/SignInput';
import '../../style/odocWithdraw.less';
import { inject, observer } from 'mobx-react';
import { Button } from 'antd';

const { WeaTable } = WeaTableNew;
const { getLabel } = WeaLocaleProvider;

/**
 * 公文流程撤回主体内容
 */
@inject('withDrawStore')
@observer
class WithDrawContent extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.visible != nextProps.visible && nextProps.visible) {
      this.loadData();
    }
  }
  componentWillMount() {
    if (this.props.visible) {
      this.loadData();
    }
  }

  loadData() {
    const { allstore } = this.props;
    const { withDrawStore, odocCompsStore } = allstore;
    let params = {};
    try {
      let { requestParam = {} } = odocCompsStore;
      requestParam = toJS(requestParam);
      delete requestParam.markInfo;
      params = { ...requestParam };
    } catch (e) {
      params = { requestid: 11918915 };
    }

    const { initLoad } = withDrawStore;
    initLoad(params);
  }
  render() {
    const { coreparams, load, refid, loadSignInput = false, signinput } = this.getWithDrawStore();
    const { showSignInput = false, showMyTodo = true, showDone = true } = coreparams;
    coreparams.refid = refid;
    coreparams.premarkinfo = signinput;
    if (load) {
      return (
        <div className={'widthdrawcontent'} >
          <div>
            {showDone && this.getDoneCom()}
            {/* {showMyTodo && this.getMyTodoContetn()} */}
          </div>
          <div>
            {showMyTodo && this.getMyTodoContetn()}
            {loadSignInput && showSignInput &&
              <OdocSignInput ecId={`${this && this.props && this.props.ecId || ''}_OdocSignInput@c4cnn3`}
                coreparams={coreparams}
                allstore={this.props.allstore}
                allFunction={{ changecallback: this.changecallback }}
              />}
          </div>
        </div>);
    }
    return (
      <div className={'widthdrawcontent'} />);
  }

  getDoneCom = () => {
    const { otherparams = {} } = this.props;
    const { tableStore, submitparams, changeSubmitparams, coreparams } = this.getWithDrawStore();
    const { showSignInput = true, showMyTodo = true } = coreparams;

    let { height = 800, doneOtherParams = {} } = otherparams;
    height -= 100;// 减去自身因减去的高度
    if (showSignInput) {
      height -= 303;// 签字意见高度
    }
    if (showMyTodo) {
      height -= 25;// 撤回我的待办高度
    }
    if (height < 100) height = 100;
    const { doneSk = [] } = submitparams;
    const { doenTableStore } = tableStore;


    let doneparams = {
      pagination: false,
      // title:`${getLabel(	511633,"请选择要撤回的在办人员:")}（${getLabel(	511632, "已选：")}${doneSk.length} / ${doenTableStore.count}）`,
      hasOrder: true,
      scroll: { y: (height) },
      ...doneOtherParams,
      comsWeaTableStore: doenTableStore,
      rowSelection: {
        onChange: (v) => {
          changeSubmitparams({ doneSk: toJS(v) });
        },
      },
    };
    return (<div><WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@xcmwr3`}
      datas={[{
        title: `${getLabel(511633, '请选择要撤回的在办人员:')}（${getLabel(511632, '已选：')}${doneSk.length} / ${doenTableStore.count}）`,
        key: '1',
      }]}
      keyParam="key" // 主键
      searchType={['advanced']}
      searchsAd={this.getSearchs()}
      advanceHeight={120}
      buttonsAd={this.getButtonsAdQuick()}
      showSearchAd={this.state.showSearchAd}
      setShowSearchAd={this.setShowSearchAd}

    /><WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@r21u7s`} {...doneparams} /></div>);
  }

  setShowSearchAd = (bool) => {
    this.setState({
      showSearchAd: bool,
    });
  }

  getButtonsAdQuick = () => {
    const { allstore } = this.props;
    const { withDrawStore } = allstore;
    const { form, reloadLoad } = withDrawStore;
    return [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@hmwn84@search`}
        type="primary"
        onClick={() => {
          reloadLoad(true);
          this.setState({ showSearchAd: false });
        }}
      >
        {getLabel(388113, '搜索')}
      </Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@swovoc@reset`} type="ghost" onClick={() => { form.resetForm(); }}>
        {getLabel(2022, '重置')}
      </Button>,
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@okijlg@cancel`} type="ghost" onClick={() => this.setState({ showSearchAd: false })}>
        {getLabel(201, '取消')}
      </Button>,
    ];
  }

	/**
	 * 高级搜索框信息
	 */
  getSearchs = () => {
    const { allstore } = this.props;
    const { withDrawStore } = allstore;
    const { condition, form } = withDrawStore;
    let groups = [];
    if (form.isFormInit) {
      condition.map((group, index) => {
        groups.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@urawk1@${index}`}
          needTigger={false}
          // title={group.title}
          showGroup={group.defaultshow}
          items={group.items.map(field => ({
            com: (
              <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@tcx4k3@${field.domkey[0]}`}
                label={field.label}
                labelCol={{ span: field.labelcol }}
                wrapperCol={{ span: field.fieldcol }}
              >
                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@yroo0n@${field.domkey[0]}`} fieldConfig={field} form={form} />
              </WeaFormItem>
            ),
            col: field.conditionType.toUpperCase() == WeaTools.types.CASCADER ? 1 : 2,
          }))}
        />);
      });
    }
    return groups;
  }

	/**
	 * 恢复我的办理权限
	 * @returns {*}
	 */
  getMyTodoContetn = () => {
    const { submitparams, changeSubmitparams, coreparams } = this.getWithDrawStore();
    const { myTodo } = submitparams;
    const { showMyTodo = true } = coreparams;
    let checkParams = {
      ref: '',
      viewAttr: showMyTodo ? 2 : 1,
      content: getLabel(511630, '恢复我的办理权限'),
      value: myTodo,
      onChange: (v) => {
        changeSubmitparams({ myTodo: toJS(v) });
      },
    };
    return <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@zsog52`} {...checkParams} />;
  }

  changecallback = (v) => {
    const { changeSubmitparams } = this.getWithDrawStore();
    changeSubmitparams(v);
  }


  getWithDrawStore = () => {
    const { allstore } = this.props;
    const { withDrawStore } = allstore;
    return withDrawStore;
  }
}
export default WeaTools.tryCatch(React,
  props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@56d9sq`} msg={props.error ? props.error : getLabel(385218, '对不起，该页面异常，请联系管理员！')} />,
  { error: '' },
)(WithDrawContent);
